<?php
// +----------------------------------------------------------------------
// | Description: 客户
// +----------------------------------------------------------------------
// | Author:  Zl
// +----------------------------------------------------------------------
namespace app\crm\model;

use app\admin\model\User;
use app\common\traits\ActionRecordTrait;
use think\facade\Db;
use app\admin\model\Common;
use think\response\Json;
use think\Validate;
use app\crm\logic\WhereLogic;

class CustomerTj extends Common
{
    use ActionRecordTrait;
    /**
     * 为了数据库的整洁，同时又不影响Model和Controller的名称
     * 我们约定每个模块的数据表都加上相同的前缀，比如CRM模块用crm作为数据表前缀
     */
    protected $name = 'customer';

    /**
     * 设置客户的年龄
     */
    public function setCustomerAge(): bool
    {
        $field = 'id,birthday';
        // try {
            //处理更新的数据
            $list  = $this -> field($field) -> select() -> toArray();
            $saveAll = array();
            foreach($list as $key => $val){
                if ($val['birthday']) {
                    $setData            = array();
                    $setData['age']     = bcsub(date('Y'),date('Y',$val['birthday']));
                    $setData['id']      = $val['id'];
                    $saveAll[]          = $setData;
                }
            }

            if($saveAll){
                // print_r($setData);exit;
                $res = $this -> saveAll($saveAll);
                if ($res === false) {
                   $this->error = '更新失败';
                   return array();
                }
            }
        // } catch (\Exception $e) {
        //     $this->error = '更新失败2';
        //     return false;
        // }
        return true;
    }
}
